# -*- coding: utf-8 -*-
# Version: Python 3.9.5
# Author: TRIX
# Date: 2021-09-08 18:03:08
# Use: 按照列表顺序 合并多个pdf

import PyPDF3
def merge_pdfs(*pdf_files):
	pdfs_list=list(pdf_files)
	new_pdf=PyPDF3.PdfFileWriter()#新建一个pdf
	newfile=open('merged_{}.pdf'.format(len(pdfs_list)),'wb')
	total_num=0

	for i,pdf in enumerate(pdfs_list):
		with open(pdf,'rb') as f:
			origin_pdf=PyPDF3.PdfFileReader(f)
			total_num+=origin_pdf.numPages
			print('第{}个pdf有{}页'.format(i+1,origin_pdf.numPages))
			for n in range(origin_pdf.numPages):#合并每个pdf
				new_pdf.addPage(origin_pdf.getPage(n))
			new_pdf.write(newfile)#写入新建pdf
	newfile.close()
	print('合并后pdf共{}页'.format(total_num))

merge_pdfs(r'D:\[CodePrograms]\Python_Primary_Programs\document_processing\pdf_processing\add_watermark\提问的智慧.pdf',
	r'D:\[estudiar]\english\新东方四级\01 导学\导学.pdf',
	r'E:\[Estudiar]\AI教程2x16-7g\AI教程\01AI教程\常用颜色色谱表.pdf')

